<template>
    <el-row class=" min-h-screen bg-indigo-400">
        <el-col :lg="16" class=" flex items-center justify-center flex-col">
            <div class=" font-bold text-5xl text-light-50 mb-3">欢迎光临</div>
            <div class=" text-gray-300 text-sm">此站点是南京网博 Vue3+Vite 网站后台</div>
        </el-col>
        <el-col :lg="8" class=" bg-light-50 flex items-center justify-center flex-col">
            <div class=" font-bold text-3xl text-gray-700">欢迎回来</div>
            <div class=" flex items-center justify-center my-5 text-gray-300 space-x-2">
                <span class="h-[1px] w-10 bg-gray-200"></span>
                <span>账号密码登录</span>
                <span class="h-[1px] w-10 bg-gray-200"></span>
            </div>
            <el-form :rules="rules" :model="form" class="w-[260px]" ref="formRef">
                <!-- 使用图标 -->
                <el-form-item prop="username">
                    <el-input v-model="form.username" placeholder="请输入用户名">
                        <template #prefix>
                            <el-icon class="el-input__icon">
                                <!-- 点击复制过来的代码 -->
                                <el-icon>
                                    <User />
                                </el-icon>
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="form.password" placeholder="请输入密码">
                        <template #prefix>
                            <el-icon class="el-input__icon">
                                <!-- 点击复制过来的代码 -->
                                <el-icon>
                                    <Lock />
                                </el-icon>
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" color="#626AEF" round class="w-[260px]" @click="onSubmit">登 录</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>
<script setup>
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/user'
import { ElNotification } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()


const formRef = ref(null)

const form = reactive({
    username: '',
    password: ''
})

const onSubmit = () => {
    formRef.value.validate((result) => {
        // 当表单校验通过时返回true,否则返回false
        // console.log(result)
        if (!result) {
            return false;
        }
        // alert("校验通过")
        console.log("username:" + form.username + ",password:" + form.password)
        login(form.username, form.password)
            .then(result => {
                console.log(result)
                const code = result.data.responseCode;
                const message = result.data.message;
                if (code == "2001") {
                    ElNotification({
                        title: 'Success',
                        message,
                        type: 'success',
                    })
                    router.push("/showMain")
                } else {
                    ElNotification({
                        title: 'Error',
                        message,
                        type: 'error',
                    })
                }
            })
    })
}

const rules = {
    username: [
        { required: true, message: '用户名不能为空', trigger: 'blur' },
        { min: 5, max: 10, message: '用户名长度必须为5-10个字符', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
        { pattern: /^\w{5,10}$/, message: '密码由5-10位英文字母、数字或者下划线组成', trigger: 'blur' }
    ]
}
</script>